import VueRouter from "vue-router";
import useNProgress from "@/hooks/Nprpgress";
import store from '@/store/index'
const NProgress = useNProgress();
import Vue from "vue";
// //获取原型对象上的push函数
// const originalPush = VueRouter.prototype.push
// //修改原型对象中的push方法
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login.vue"),
    meta: { title: "Login" },
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("@/Home.vue"),
    redirect: "/home/index",
    meta: { title: "Home" },
    children: [
      {
        path: "index",
        name: "index",
        component: () => import("@/views/index.vue"),
        meta:{title:'首页'}
      },
      {
        path: "tab1",
        name: "tab1",
        component: () => import("@/views/LeftMenu_pages/tab1.vue"),
        meta:{title:'tab1'}
      },
      {
        path: "tab2",
        name: "tab2",
        component: () => import("@/views/LeftMenu_pages/tab2.vue"),
        meta:{title:'tab2'}
      },
      {
        path: "tab3",
        name: "tab3",
        component: () => import("@/views/LeftMenu_pages/tab3.vue"),
        meta:{title:'tab3'}
      },
      {
        path: "tab4",
        name: "tab4",
        component: () => import("@/views/LeftMenu_pages/tab4.vue"),
        meta:{title:'tab4'}
      },
      {
        path: "tab5",
        name: "tab5",
        component: () => import("@/views/LeftMenu_pages/tab5.vue"),
        meta:{title:'tab5'}
      },
      {
        path: "tab6",
        name: "tab6",
        component: () => import("@/views/LeftMenu_pages/tab6.vue"),
        meta:{title:'tab6'}
      },
      {
        path: "tab7",
        name: "tab7",
        component: () => import("@/views/LeftMenu_pages/tab7.vue"),
        meta:{title:'tab7'}
      },
      {
        path: "tab8",
        name: "tab8",
        component: () => import("@/views/LeftMenu_pages/Chatroom.vue"),
        meta:{title:'tab8'}
      },
    ],
  },
  {
    path:"/PersonCenter",
    name:"persnoal",
    component:()=> import("@/views/PersonlCenter.vue"),
    meta: { title: "center" },
  },
  {
    path:"/Myphotos",
    name:"Myphotos",
    component:()=> import("@/views/Myphotos.vue"),
    meta: { title: "center" },
  },
];

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  //开启进度条
  NProgress.start();
  const token = window.sessionStorage.getItem('token')
  if (to.name !== "Login" && !token) {
    next("/login");
  } else {
    console.log("to",to)
    store.dispatch('commitRouterlinksync',{title:to.meta.title,path:to.path}),
    next();
  }
});

router.afterEach(() => {
  //关闭进度条
  NProgress.done();
});

export default router;
